using MySql.Data.EntityFrameworkCore.Extensions;
using Microsoft.EntityFrameworkCore;
using CMSSample.Models;
using JetBrains.Annotations;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CMSSample.Models
{
    public class InfoContext : DbContext
    {
        private readonly DbContextOptions<InfoContext> options;
        public InfoContext(DbContextOptions<InfoContext> options) : base(options)
        {
            this.options = options;
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            modelBuilder.Entity<Publisher>(entity =>
            {
                entity.HasKey(e => e.ID);
                entity.Property(e => e.Name).IsRequired();
                entity.HasMany(e => e.Books).WithOne(p => p.Publisher);
            });
            modelBuilder.Entity<Book>(entity =>
            {
                entity.HasKey(e => e.ISBN);
                entity.Property(e => e.Title).IsRequired();
                entity.HasOne(d => d.Publisher).WithMany(p => p.Books);
            });

        }
        public DbSet<Book> Book { get; set; }
        public DbSet<Publisher> Publisher { get; set; }

        public List<Book> SeedBook()
        {
            List<Book> books = new List<Book>();
            for (int i = 0; i < 10; i++)
            {
                books.Add(new Book
                {
                    ISBN = $"{i}",
                    Title = $"入门到精通{i}",
                    Author = $"{i}",
                    Language = $"{i++}",
                    Pages = i++,
                    Publisher = new Publisher { ID = i, Name = $"大禹霸霸{i}号" }
                });
            }
            return books;
        }

    }
    public class InfoViewModel
    {
        public List<Book> Books { get; set; }
        private readonly InfoContext context;
        public InfoViewModel(InfoContext context)
        {
            this.context = context;
        }
    }

    public class Book
    {
        public string ISBN { get; set; }
        public string Title { get; set; }
        public string Author { get; set; }
        public string Language { get; set; }
        public int Pages { get; set; }
        public virtual Publisher Publisher { get; set; }
    }

    public class Publisher
    {
        public Publisher(int iD, string name)
        {
            this.ID = iD;
            this.Name = name;

        }
        public Publisher() { }
        public int ID { get; set; }
        public string Name { get; set; }
        public virtual ICollection<Book> Books { get; set; }
    }
}